package cn.green.core.components.store.field;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.green.core.components.exceptions.FieldCheckTypeNullException;
import cn.green.core.components.field.FieldCheckTypeCom;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className FieldCheckTypeStore
 * @description 字段校验仓库
 * @date 2024/11/18 9:22
 */
public class FieldCheckTypeStore {

    private static final Map<String, FieldCheckTypeCom> FIELD_CHECK_TYPE_MAP = new HashMap<>();

    private static final JSONArray OPTIONS = new JSONArray();

    public static void put(FieldCheckTypeCom type) {
        FIELD_CHECK_TYPE_MAP.put(type.typeCode(), type);
        JSONObject option = new JSONObject();
        option.put("label", type.typeName());
        option.put("value", type.typeCode());
        OPTIONS.add(option);
    }

    public static FieldCheckTypeCom get(String typeCode) {
        if (!FIELD_CHECK_TYPE_MAP.containsKey(typeCode)) {
            try {
                throw new FieldCheckTypeNullException(typeCode);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

        }
        return FIELD_CHECK_TYPE_MAP.get(typeCode);
    }

    public static JSONArray options() {
        return OPTIONS;
    }
}
